package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.repository.IUserInfoTemplateRepository;
import cn.wolfcode.luowowo.search.service.IUserInfoSearchService;
import cn.wolfcode.luowowo.search.template1.UserInfoTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

/**
 * Created by Administrator on 2019/7/15.
 */
@Service
public class UserInfoSearchServiceImpl implements IUserInfoSearchService{
    @Autowired
    private ElasticsearchTemplate template;
    @Autowired
    private IUserInfoTemplateRepository dao;
    @Autowired
    private TransportClient client;

    @Override
    public void creatIndex() {
        template.createIndex(UserInfoTemplate.class);
        template.getMapping(UserInfoTemplate.class);
    }

    @Override
    public void save(UserInfoTemplate template) {
        dao.save(template);
    }

    @Override
    public Page<UserInfoTemplate> query(SearchQueryObject qo) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        TermQueryBuilder query;
        if (qo.getDestId()!=-1){
            query=QueryBuilders.termQuery("destName", qo.getDestName());
            boolQuery.must(query);
        }
        return dao.search(boolQuery,qo.getPageablewithoutSort());
    }
}
